#pragma once
#include <string>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
#include <sstream>

using namespace std;

class observatory
{
private:
	struct data
	{
		vector<int> observatories;

		data(int observatory = -1) 
		{
			if (observatory != -1)
				observatories.push_back(observatory);
		}
	};

	map<string, data> m;
public:

	observatory()
	{
	}

	~observatory()
	{
	}

	void read_from_file(string filename)
	{
		string str, row;
		if (!m.empty())
			m.clear();
		try
		{
			ifstream fin(filename);
			while (getline(fin, row))
			{
				istringstream iss(row);
				getline(iss, str, ' ');
				int observatory;
				istringstream iss2(str);
				iss2 >> observatory;
				getline(iss, str);
				if (str.compare("") != 0)
				{
					auto it = m.find(str);
					if (it != m.end())
						it->second.observatories.push_back(observatory);
					else
						m.emplace(str, data(observatory));
				}
			}
			fin.close();
		}
		catch (exception e)
		{
			cout << e.what() << endl;
		}
	}

	void print()
	{
		for (auto &d : m)
		{
			if (d.second.observatories.size() >= 2)
			{
				cout << "Tid: " << d.first.substr(0, 8) << " ";
				cout << "Riktning " << d.first.substr(9, 7) << " ";
				cout << "Observatorier ";
				for (auto &o : d.second.observatories)
					cout << o << " ";
				cout << endl;
			}
		}
	}
};

